package com.mars.module.admin.controller;


import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.mars.common.enums.BusinessType;
import com.mars.common.result.R;
import com.mars.common.util.ExcelUtils;
import io.swagger.annotations.Api;
import com.mars.framework.annotation.RateLimiter;
import com.mars.module.admin.entity.SysDepartment;
import com.mars.framework.annotation.Log;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import com.mars.common.response.PageInfo;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.service.ISysDepartmentService;
import com.mars.module.admin.request.SysDepartmentRequest;

import javax.servlet.http.HttpServletResponse;

/**
 * 科室控制层
 *
 * @author caiJin
 * @date 2024-05-09
 */
@Slf4j
@AllArgsConstructor
@RestController
@Api(value = "科室接口管理",tags = "科室接口管理")
@RequestMapping("/admin/sysDepartment" )
public class SysDepartmentController {

    private final ISysDepartmentService iSysDepartmentService;

    /**
     * 分页查询科室列表
     */
    @ApiOperation(value = "分页查询科室列表")
    @PostMapping("/pageList")
    public R<PageInfo<SysDepartment>> pageList(@RequestBody SysDepartmentRequest sysDepartmentRequest) {
        return R.success(iSysDepartmentService.pageList(sysDepartmentRequest));
    }

    /**
     * 获取科室详细信息
     */
    @ApiOperation(value = "获取科室详细信息")
    @GetMapping(value = "/query/{id}")
    public R<SysDepartment> detail(@PathVariable("id") Long id) {
        return R.success(iSysDepartmentService.getById(id));
    }

    /**
     * 新增科室
     */
    @Log(title = "新增科室", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "新增科室")
    @PostMapping("/add")
    public R<Void> add(@RequestBody SysDepartmentRequest sysDepartmentRequest) {
        iSysDepartmentService.add(sysDepartmentRequest);
        return R.success();
    }

    /**
     * 修改科室
     */
    @Log(title = "修改科室", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改科室")
    @PostMapping("/update")
    public R<Void> edit(@RequestBody SysDepartmentRequest sysDepartmentRequest) {
        iSysDepartmentService.update(sysDepartmentRequest);
        return R.success();
    }

    /**
     * 删除科室
     */
    @Log(title = "删除科室", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除科室")
    @PostMapping("/delete/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        iSysDepartmentService.deleteBatch(Arrays.asList(ids));
        return R.success();
    }

    /**
    * 导出科室
    *
    * @param response response
    * @throws IOException IOException
    */
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response,@RequestBody SysDepartmentRequest sysDepartmentRequest) throws IOException {
        List<SysDepartment> list = iSysDepartmentService.list(sysDepartmentRequest);
        ExcelUtils.exportExcel(list, SysDepartment.class, "科室信息", response);
    }
}
